package com.leenleda.ips.widget;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.leenleda.ips.R;
import com.leenleda.ips.databinding.DialogAlertBinding;

public class DialogAlert {
    private Dialog dialog;
    private DialogAlertBinding binding;
    private OnClickListener listener;

    public DialogAlert(final Context context) {
        binding = DialogAlertBinding.inflate(LayoutInflater.from(context));
        dialog = new Dialog(context, R.style.custom_dialog);// 创建自定义样式dialog

        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        window.setAttributes(params);
        window.setGravity(Gravity.CENTER);

        dialog.setCancelable(true);// 可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog不消失
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));// 设置布局

        binding.dialogSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (listener != null)
                    listener.onDismiss();
            }
        });

        binding.dialogSure.setFocusable(true);
        binding.dialogSure.requestFocus();
    }

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }
    public void showWeb(String data) {
        currentTime = System.currentTimeMillis() / 1000 + 11;
        binding.dialogContent.setText(data);
        try {
            if (!dialog.isShowing()) {
                dialog.show();
            }
        } catch (Exception e) {
        }
    }

    public void show(String data) {
        currentTime = System.currentTimeMillis() / 1000 + 11;
        binding.dialogContent.setText(data);
        try {
            if (!dialog.isShowing()) {
                dialog.show();
            }
        } catch (Exception e) {
        }
    }

    private long currentTime = 0;
    public void show(){
        long time = System.currentTimeMillis() / 1000;
        int left = (int) (currentTime - time);
        if (left == 11)
            left = 10;

        binding.dialogSure.setText("确定(" + left + ")");
        if (left < 1)
            dismiss();
    }

    public void dismiss() {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    public interface OnClickListener {
        void onDismiss();//点击列表监听
    }

}
